package com.ibm.commerce.url.tests;

/*
 *-----------------------------------------------------------------
 * IBM Confidential
 *
 * OCO Source Materials
 *
 * WebSphere Commerce
 *
 * (C) Copyright IBM Corp. 2009
 *
 * The source code for this program is not published or otherwise
 * divested of its trade secrets, irrespective of what has
 * been deposited with the U.S. Copyright Office.
 *-----------------------------------------------------------------
 */


//Import the task libraries for use in this test script



import junit.framework.Test;
import junit.framework.TestSuite;

import com.ibm.commerce.url.harness.ToolingTestCase;
import com.ibm.commerce.url.harness.URLTestTask;
import com.ibm.commerce.url.tasks.accelerator.*;
import com.ibm.commerce.url.tasks.mls.GoogleSearch;
import com.ibm.commerce.url.tasks.orgadminconsole.*;
import com.ibm.commerce.url.tasks.cmc.*;
import com.ibm.commerce.url.tooling.*;
import com.ibm.commerce.url.util.ContentSearcher;
import com.ibm.commerce.url.util.Log;
import com.ibm.wc.test.dbutility.DBInvoker;

public class TestCaseSample extends ToolingTestCase {

	/** Specify a data file **/
	public final String dataFileName = "data/SampleTest_Data.xml";
	
	DBInvoker dbi;
	
	public TestCaseSample(String method) {
		super(method);
	}
	
	public static Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new TestCaseSample("testSimplestTestCase"));
		suite.addTest(new TestCaseSample("testOrgAdminConsole"));
		suite.addTest(new TestCaseSample("testCMCFileUpload"));
		suite.addTest(new TestCaseSample("testOtherFeatures"));
		return suite;
	}
	
	/**
	 * Perform setup operations before the test is executed.
	 */
	public void setUp() throws Exception {
		super.setUp();
		dbi = new DBInvoker();

		//Set the data file
		setDataFile(dataFileName);
	}
	
	public void testSimplestTestCase() throws Exception {
		StartTestCase("testSimplestTestCase");		
		// AcceleratorLogon
		AcceleratorLogon.execute();
		// AcceleratorStoreAndLangSelect
		AcceleratorStoreAndLangSelect.execute();
		// AcceleratorLogoff
		AcceleratorLogoff.execute();
	}
	
	public void testOrgAdminConsole() throws Exception {
		StartTestCase("testOrgAdminConsole");		
		OrgAdminConsoleLogon.execute();
		for(int i=0; i<2; i++){
			CreateNewUser.execute();
			browserContext.addInputParam(
					AssignRoleToUser.INPUT_PARAMETER_USERID,
					browserContext.getOutputParam(CreateNewUser.OUTPUT_PARAMETER_USERID)
					);
			AssignRoleToUser.execute();
		}
		OrgAdminConsoleLogoff.execute();
	}
	
	public void testCMCFileUpload() throws Exception {
		StartTestCase("testCMCFileUpload");
		CMCUploadAttachment.execute();
		
	}
	
	public void testOtherFeatures() throws Exception {
		StartTestCase("testOtherFeatures");

		// high level task invoke
		Accelerator.logon("wcsadmin", "wcs1admin");
		
		// get output of the last task/tooling method call
		assertEquals("wcs1admin", browserContext.getOutputParam(AcceleratorLogon.INPUT_PARAMETER_NAME_LOGONPASSWORD));
		
		GUIValidate("International Business Machines");
		
		// Extract string from response text
		String mcurl = browserContext.strSearchReg("name=\"mcurl\" value=\"(.*)\"/>", ContentSearcher.FLAG_EXTRACT_FROM_GROUP);
		assertEquals(mcurl, "MerchantCenterView?XMLFile=common.merchantCenter");
				
		String mcurl2 = browserContext.strSearchDelimited("name=\"mcurl\" value=\"", "\"/>");
		assertEquals(mcurl2, "MerchantCenterView?XMLFile=common.merchantCenter");
		
		// get parameter from global context
		globalContext.getParam("storeId");

		// get output parameter from browser context
		assertEquals("wcsadmin", browserContext.getOutputParam(AcceleratorLogon.OUTPUT_PARAMETER_NAME_LOGONNAME));

//
//		// get value from xml response by xpath
//		browserContext.getValueFromXMLbyXPath("/element1/element2");
//
//		// get values from xml response by name
//		browserContext.getValuesFromXMLbyName("logonId");
//
//		// use log
		Log.info("I am a test log string.");
//
//		// access db
//		dbi.executeQuery("select count(*) from userreg");
//
//		// wait for 1 second
		waitTime(1000);

	}
	
	public void testAccLogon() throws Exception {
		StartTestCase("testAccLogon");

		// get Accelerator tooling instance
		Accelerator.logon("wcsadmin", "wcs1admin");
		
		// get output of the last task/tooling method call
		assertEquals("wcs1admin", browserContext.getOutputParam(AcceleratorLogon.INPUT_PARAMETER_NAME_LOGONPASSWORD));
		
		GUIValidate("International Business Machines");
	}
	
	public void testAccE2EFlow() throws Exception {
		StartTestCase("testAccE2EFlow");		
		// AcceleratorLogon
		AcceleratorLogon.execute();
		// AcceleratorStoreAndLangSelect
		AcceleratorStoreAndLangSelect.execute();
		// AcceleratorLogoff
		AcceleratorLogoff.execute();
	}

	public void testAccFileUpload() throws Exception {
		StartTestCase("testAccFileUpload");
		// AcceleratorLogon
		AcceleratorLogon.execute();	
		// AcceleratorStoreAndLangSelect
		AcceleratorStoreAndLangSelect.execute();
		// AcceleratorUploadAttachment
		AcceleratorUploadAttachment.execute();
		// AcceleratorLogoff
		AcceleratorLogoff.execute();
	}

	public void testAcc4() throws Exception {
		StartTestCase("testAcc4");		
		AcceleratorLogon.execute();	
	}
	public void testAcc5() throws Exception {
		StartTestCase("testAcc5");		
		AcceleratorLogon.execute();	
		AcceleratorLogon.execute();	
		AcceleratorLogon.execute();	
		AcceleratorLogon.execute();	
	}
	
	public void testGoogle() throws Exception{
		StartTestCase("testGoogle");	
		browserContext.WebHit("http://www.google.com", URLTestTask.WEB_URL);
	}
	
	public void testMultiLanguageSupport() throws Exception{
		StartTestCase("testGoogle");
		GoogleSearch.execute();
		GUIValidate("汉字");
		
		setDataBlock("testChinese");
		Log.info(getInputParameter("charactor"));
	}
	
	/**
	 * Demo on how to use ID independent Approach (IDA) in web layer automation tool.
	 * <hr/>
	 * The input value in data file should use string such as ValueOf_NAME;, 
	 * and the ID independent approach will replace the ValueOf_NAME; into actual IDs.<br/><br/>
	 * <b>Note:</b><br/>
	 * 	1. Should specify the correct business model and store type in configuration file.<br/>
	 *  2. You must have Q: disk mapped on your client before using IDA.<br/>
	 * <br/>
	 * <b>Example:</b><br/>
	 * For input data: &lt;Parameter name="inputName" value="ValueOf_Power;"/&gt;<br/>
	 * If the Power item's ID in IDA common data file is 50400000002, then the actual input at runtime will be:
	 * &lt;Parameter name="inputName" value="50400000002"/&gt;
	 * <br/>
	 * @throws Exception
	 */
	public void testIDA() throws Exception {
		StartTestCase("testIDA");		
		setDataBlock("getPowerId");
		Log.info("The ID of " + getInputParameter("inputName"));
	}
	
	/**
	 * Perform teardown operations after the test is done, whether it is successful or not.
	 * If need to override this method, must firstly call super.tearDown();
	 */
//	public void tearDown() throws Exception {
//		super.tearDown();
//		your own tearDown codes
//	}
	
}